<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="wid">
        <el-input
          v-model="queryParams.wid"
          placeholder="请输入微盟用户ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="级别名" prop="levelName">
        <el-input
          v-model="queryParams.levelName"
          placeholder="请输入级别名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!--      <el-form-item label="订单状态" prop="orderStatus">-->
      <!--        <el-select-->
      <!--          v-model="queryParams.orderStatus"-->
      <!--          placeholder="请选择订单状态"-->
      <!--          clearable-->
      <!--          class="!w-240px"-->
      <!--        >-->
      <!--          <el-option-->
      <!--            v-for="dict in getIntDictOptions(DICT_TYPE.WO_ORDER_STATUS)"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="支付状态" prop="paymentStatus">-->
      <!--        <el-select-->
      <!--          v-model="queryParams.paymentStatus"-->
      <!--          placeholder="请选择支付状态"-->
      <!--          clearable-->
      <!--          class="!w-240px"-->
      <!--        >-->
      <!--          <el-option-->
      <!--            v-for="dict in getIntDictOptions(DICT_TYPE.WO_ORDER_PAY_STATUS)"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="售后状态" prop="rightsStatus">-->
      <!--        <el-select-->
      <!--          v-model="queryParams.rightsStatus"-->
      <!--          placeholder="请选择售后状态"-->
      <!--          clearable-->
      <!--          class="!w-240px"-->
      <!--        >-->
      <!--          <el-option-->
      <!--            v-for="dict in getIntDictOptions(DICT_TYPE.WO_ORDER_RIGHT_STATUS)"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="结算状态" prop="settleStatus">
        <el-select
          v-model="queryParams.settleStatus"
          placeholder="请选择结算状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.WO_ORDER_SETTLE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          plain-->
        <!--          @click="openForm('create')"-->
        <!--          v-hasPermi="['wo:order:create']"-->
        <!--        >-->
        <!--          <Icon icon="ep:plus" class="mr-5px" /> 新增-->
        <!--        </el-button>-->
        <el-button
          type="primary"
          plain
          @click="openImportForm"
          :loading="importLoading"
          v-hasPermi="['wo:order:create']"
        >
          <Icon class="mr-5px" icon="ep:zoom-in" />
          导入
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['wo:order:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      style="width: 100%"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <!-- 子表的列表 -->
      <el-table-column type="expand">
        <template #default="scope">
          <el-tabs model-value="orderItem">
            <el-tab-pane label="订单明细" name="orderItem">
              <OrderItemList :order-id="scope.row.id" />
            </el-tab-pane>
            <el-tab-pane label="利润分配" name="reward">
              <RewardList :order-id="scope.row.id" />
            </el-tab-pane>
            <el-tab-pane label="团队业绩" name="teamShare">
              <TeamShareList :order-id="scope.row.id" />
            </el-tab-pane>
          </el-tabs>
        </template>
      </el-table-column>
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column width="160px" label="订单编号" align="center" prop="orderNo" />
      <el-table-column width="120px" label="用户ID" align="center" prop="wid" />
      <el-table-column label="用户名" align="center" prop="name" />
      <el-table-column width="120px" label="手机号" align="center" prop="phone" />
      <el-table-column width="120px" label="级别ID" align="center" prop="levelId" />
      <el-table-column label="级别名" align="center" prop="levelName" />
      <el-table-column label="订单金额" align="center" prop="orderPrice" />
      <el-table-column label="实际金额" align="center" prop="orderPay" />

      <!--      <el-table-column width="100px" label="订单状态" align="center" prop="orderStatus">-->
      <!--        <template #default="scope">-->
      <!--          <dict-tag :type="DICT_TYPE.WO_ORDER_STATUS" :value="scope.row.orderStatus" />-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="结算状态" width="100px" align="center" prop="settleStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.WO_ORDER_SETTLE_STATUS" :value="scope.row.settleStatus" />
        </template>
      </el-table-column>
      <!--      <el-table-column width="100px" label="支付状态" align="center" prop="paymentStatus">-->
      <!--        <template #default="scope">-->
      <!--          <dict-tag :type="DICT_TYPE.WO_ORDER_PAY_STATUS" :value="scope.row.paymentStatus" />-->
      <!--        </template>-->
      <!--      </el-table-column>-->

      <!--      <el-table-column width="100px" label="售后状态" align="center" prop="rightsStatus">-->
      <!--        <template #default="scope">-->
      <!--          <dict-tag :type="DICT_TYPE.WO_ORDER_RIGHT_STATUS" :value="scope.row.rightsStatus" />-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />

      <el-table-column fixed="right" width="120px" label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            v-if="scope.row.settleStatus == 0"
            :loading="confirmLoading"
            type="primary"
            @click="settle(scope.row.id)"
            v-hasPermi="['wo:order:settle']"
          >
            结算
          </el-button>

          <el-button
            link
            v-if="scope.row.settleStatus == 1"
            :loading="confirmLoading"
            type="primary"
            @click="confirm(scope.row.id)"
            v-hasPermi="['wo:order:confirm']"
          >
            确认
          </el-button>

          <el-button
            link
            :loading="confirmLoading"
            v-if="scope.row.settleStatus == 0"
            type="danger"
            @click="openCancel(scope.row.id)"
            v-hasPermi="['wo:order:update']"
          >
            取消
          </el-button>
          <!--          <el-button-->
          <!--            link-->
          <!--            type="primary"-->
          <!--            @click="openForm('update', scope.row.id)"-->
          <!--            v-hasPermi="['wo:order:update']"-->
          <!--          >-->
          <!--            编辑-->
          <!--          </el-button>-->
          <!--          <el-button-->
          <!--            link-->
          <!--            type="danger"-->
          <!--            @click="handleDelete(scope.row.id)"-->
          <!--            v-hasPermi="['wo:order:delete']"-->
          <!--          >-->
          <!--            删除-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrderForm ref="formRef" @success="getList" />

  <ImportOrderForm @success="getList" ref="importFormRef" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OrderApi, OrderVO } from '@/api/wo/order'
import OrderForm from './OrderForm.vue'
import OrderItemList from './components/OrderItemList.vue'
import ImportOrderForm from '@/views/wo/order/components/ImportOrderForm.vue'
import RewardList from './components/RewardList.vue'
import TeamShareList from './components/TeamShareList.vue'

/** 微盟订单 列表 */
defineOptions({ name: 'WoOrder' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const importLoading = ref(false)
const list = ref<OrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderNo: undefined,
  wid: undefined,
  levelName: undefined,
  phone: undefined,
  orderStatus: undefined,
  paymentStatus: undefined,
  rightsStatus: undefined,
  createTime: [],
  settleStatus: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderApi.getOrderPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const confirm = async (id) => {
  await message.confirm('是否确认操作!')
  confirmLoading.value = true
  try {
    await OrderApi.confirmOrder({ id })
    message.success('操作成功!')
    await getList()
  } finally {
    confirmLoading.value = false
  }
}

const settle = async (id) => {
  await message.confirm('是否确认操作!')
  confirmLoading.value = true
  try {
    await OrderApi.settleOrder({ id })
    message.success('操作成功!')
    await getList()
  } finally {
    confirmLoading.value = false
  }
}

const confirmLoading = ref(false)

const openCancel = async (id) => {
  await message.confirm('是否确认操作!')
  confirmLoading.value = true
  try {
    await OrderApi.cancelOrder({ id })
    message.success('取消成功!')
    await getList()
  } finally {
    confirmLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const importFormRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

const openImportForm = (type: string, id?: number) => {
  importFormRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OrderApi.deleteOrder(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OrderApi.exportOrder(queryParams)
    download.excel(data, '微盟订单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
